package com.jowell.mall.product.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: Mr.ZJW
 * @Date: 2022-07-08 15:00
 * @Description: MQ相关配置类
 */
@Configuration
public class MQConfig {

    public static final String DIRECT_QUERY = "directQuery";
    public static final String FANOUT_QUERY = "fanoutQuery";
    public static final String DIRECT_EXCHANGE = "directExchange";
    public static final String TOPIC_QUERY = "topicQuery";
    public static final String FANOUT_EXCHANGE = "fanoutExchange";
    public static final String TOPIC_EXCHANGE = "topicExchange";

    /************************************************设置队列**********************************************/
    /**
     * direct队列
     */
    @Bean
    public Queue directQuery() {
        return new Queue(DIRECT_QUERY, true);
    }
    /**
     * fanout队列
     */
    @Bean
    public Queue fanoutQuery() {
        return new Queue(FANOUT_QUERY, true);
    }
    /**
     * topic队列
     */
    @Bean
    public Queue topicQuery() {
        return new Queue(TOPIC_QUERY, true);
    }

    /************************************************设置交换机类型**********************************************/
    /**
     * Direct交换机
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE);
    }
    /**
     * fanout交换机
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE);
    }
    /**
     * topic交换机
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }

    /************************************************队列绑定交换机**********************************************/
    /**
     * 队列绑定direct交换机
     */
    @Bean
    public Binding queueBindingDirectExchange(@Qualifier(DIRECT_QUERY) Queue queue,
                                              @Qualifier(DIRECT_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DIRECT_QUERY).noargs();
    }
    /**
     * 队列绑定fanout交换机
     */
    @Bean
    public Binding queueBindingFanoutExchange(@Qualifier(FANOUT_QUERY) Queue queue,
                                             @Qualifier(FANOUT_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(FANOUT_QUERY).noargs();
    }
    /**
     * 队列绑定topic交换机
     */
    @Bean
    public Binding queueBindingTopicExchange(@Qualifier(TOPIC_QUERY) Queue queue,
                                             @Qualifier(TOPIC_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(TOPIC_QUERY).noargs();
    }

    /**
     * 乱码配置
     */
    @Bean
    public MessageConverter getMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

}